<%--
  Created by IntelliJ IDEA.
  User: e575
  Date: 2019/6/13
  Time: 9:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<link rel="stylesheet" type="text/css" href="/common/layui/css/layui.css" media="all">
<link rel="stylesheet" type="text/css" href="/common/bootstrap/css/bootstrap.css" media="all">
<link rel="stylesheet" type="text/css" href="/common/global.css" media="all">
<link rel="stylesheet" type="text/css" href="/css/personal.css" media="all">
<link rel="stylesheet" type="text/css" href="/zTree/zTreeStyle/zTreeStyle.css">
<script type="text/javascript" src="/js/jquery2.js"></script>
<script type="text/javascript" src="/zTree/jquery.ztree.all.js"></script>
<body>
<script>
    var setting = {
        check: {
            enable: true,
            chkboxType: {"Y": "p", "N": "ps"}
        },
        async: {
            enable: true,
            dataType: "json",
            type: "get",
            url: "/menu/menulist"
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "pid",
                rootPId: "0"
            },
            key: {
                name: "functionName"
            }
        }
    };
    $(function () {
        rolelist();
        var menu = $.fn.zTree.init($("#menu"), setting);
        $("#role").change(function () {
            var rid = $("#role").val();
            $.ajax({
                type: "POST",
                url: "/menu/menulist",
                data: {
                    rid: rid
                },
                dataType: "json",
                success: function (msg) {
                    var menu = $.fn.zTree.init($("#menu"), setting, msg);
                },
                error: function () {
                    alert("失败");
                }
            });
        });
        $("#btn").on("click", function () {
            var treeObj = $.fn.zTree.getZTreeObj("menu", setting);
            var nodes = treeObj.getCheckedNodes(true);
            var nodess = new Array();
            for (var i = 0; i < nodes.length; i++) {
                nodess[i] = nodes[i].id;
            }
            var rid = $("#role").val();
            $.ajax({
                type: "POST",
                url: "/menu/menu",
                data: {
                    rid: rid,
                    nodess: JSON.stringify(nodess)
                },
                dataType: "json",
                success: function (msg) {
                    alert(msg)
                },
                error: function () {
                    alert("失败");
                }
            });
        });
    });

    function rolelist() {
        $.ajax({
            type: "POST",
            url: "/menu/rolelist",
            data: "",
            dataType: "json",
            success: function (msg) {
                $.each(msg, function (i, n) {
                    $("#role").append("<option value='" + n.id + "'>" + n.role_name + "</option>");
                })
            },
            error: function () {
                alert("失败");
            }
        });
    }
</script>
<div>
    <select id="role" class="layui-input search_input">
        <option value="">--请选择--</option>
    </select>
    <button id="btn" class="layui-btn" lay-filter="formDemo">提交修改后角色功能点</button>
</div>
<div>
    <ul id="menu" class="ztree">

    </ul>
</div>
</body>
</html>
